package com.baidu.speech.qr_code.ui.home;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import com.baidu.speech.qr_code.R;
import com.baidu.speech.qr_code.ui.notifications.Personal_signature1;
import com.google.zxing.WriterException;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;

import org.litepal.LitePal;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;


public class HomeMainivity extends AppCompatActivity {

    private ImageView iv;
    private Button bu_setup;
    //二维码的封装类
    private Code_image code_image=new Code_image();
    //二维码的第三方库zxing
    private Bitmap zxing;
    @RequiresApi(api = Build.VERSION_CODES.Q)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.home_qr_code);
        try {
            initview();
        } catch (WriterException e) {
            e.printStackTrace();
        }
    }

    //当再次运行该程序，刷新以下内容
    @RequiresApi(api = Build.VERSION_CODES.Q)
    @Override
    public void onResume() {
        super.onResume();
        try {
            initview();
        } catch (WriterException e) {
            e.printStackTrace();
        }
    }

    @RequiresApi(api = Build.VERSION_CODES.Q)
    private void initview() throws WriterException {
        iv=findViewById(R.id.iv);
        iv.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {
                Toast.makeText(HomeMainivity.this, "长按识别二维码未完成", Toast.LENGTH_SHORT).show();
                return true;
            }
        });
        //接收HomeFragment的数据extra_data
        Intent intent=getIntent();
        final String data=intent.getStringExtra("extra_data");
        try {
            Core_log pstv_map = LitePal.findLast(Core_log.class);
            //获取Head_portrait数据库最新一张照片显示出来
            File file = new File(pstv_map.getHeadshot());
            FileInputStream fil =new FileInputStream(file);
            Bitmap bitmap = BitmapFactory.decodeStream(fil);
            //中间头像转成bitmap类型
            zxing = code_image.gainBitmap(bitmap);
            //加头像把头像放到二维码里 1.头像2.获取要转化成二维码的信息3.宽4.高
            zxing = code_image.makeQRImage(zxing,data,400,400,0xFF000000);
            //图片引用zxing里的内容，zxing里的内容是data
            iv.setImageBitmap(zxing);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

        bu_setup=findViewById(R.id.bu_setup);
        //按钮点击事件
        bu_setup.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent1=new Intent(HomeMainivity.this,Home_MainActivity_setup.class);
                intent1.putExtra("extra_data1",data);
                startActivity(intent1);
            }
        });
    }

}